package com.example.springboot.init;

import com.example.springboot.interceptor.CustomInterceptor;
import com.example.springboot.interceptor.RepeatSubmitInterceptor;
import com.example.springboot.convert.GlobalDateConvert;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p> 使用 WebMvcConfigurer 注册拦截器
 * <p>
 * @author Zhang.Z
 * @version 2025-09-20
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {


    /**
     * 添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //不拦截的uri
        final String[] commonExclude = {"/hello", "/user/register"};
        registry.addInterceptor(new CustomInterceptor())
                .addPathPatterns("/api/**") //匹配 /api/ 开头的请求
                .order(1)
                .excludePathPatterns(commonExclude); // 排除路径

        registry.addInterceptor(new RepeatSubmitInterceptor())
                .addPathPatterns("/**") //匹配所有的请求，不写addPathPatterns，默认就是拦截所有请求
                .order(2)
                .excludePathPatterns(commonExclude);
    }


    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new GlobalDateConvert());
    }

//    //JSON格式 全局日期转换器配置
//    @Bean
//    public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
//        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
//        //设置日期格式
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setDateFormat(GlobalJsonDateConvert.instance);
//        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//        mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
//        //设置中文编码格式
//        List<MediaType> list = new ArrayList<MediaType>();
//        list.add(MediaType.APPLICATION_JSON_UTF8);
//        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
//        return mappingJackson2HttpMessageConverter;
//    }
}
